// pages/myBankCard/myBankCard.js

var app = getApp();
var page=1;
var selectedNews = false;
var newsList = [];

Page({
  data:{
    listdefault:1,
    cardlist: [],
    goodsId: [],
    mount: [],
    canCheck:false
  },
  //获取银行卡列表
  getCardList: function () {
    var that = this;
    var url = '/wallet_api/bankList';
    var params = {
      uid: app.globalData.userInfo.id,
      pageNo: page,
      pageSize: 20
    };
    app.HttpPostSend(url, params, function (res) {
      if (res.msg != "success") {
        app.showTipMsg(res.msg);
        selectedNews = false;
        that.setData({
          cardlist: []
        })
      } else {
        var cardlist = res.data;
        for (var i = 0; i < cardlist.length; i++) {
          cardlist[i].card_number = cardlist[i].card_number.slice(cardlist[i].card_number.length - 4, cardlist[i].card_number.length);
          newsList.push(cardlist[i]);
        }
        if (cardlist.length < 20) {
          selectedNews = false;
        } else {
          selectedNews = true;
          page += 1;
        }
        that.setData({
          cardlist: newsList
        })
        wx.hideLoading();
      }
    });
  },
  onLoad:function(options){
    // 页面初始化 options为页面跳转所带来的参数
    var that = this;
    if(options.backtype == 'withdraw'){
      that.setData({
        canCheck: true
      })
    } else {
      that.setData({
        canCheck: false
      })
    }    
  },

  //点击切换银行卡
  checkCard: function (e) {
    var index = e.currentTarget.dataset.index;
    var cardlist = this.data.cardlist;
    var info = JSON.stringify(cardlist[index]);
    var that = this;
    wx.navigateBack({
      url: '../withdraw/withdraw?info=' + info
    })
  },
  bindAddTap: function (e) {
    wx.navigateTo({
      url: '../addCard/addCard',
    })
  },
  //点击去解绑银行卡
  bindCancelTap: function (e) {
    var index = e.currentTarget.dataset.index;
    var info = JSON.stringify(this.data.cardlist[index]);
    wx.navigateTo({
      url: '../cancelCard/cancelCard?info=' + info
    })
  },
  onReady:function(){
    // 页面渲染完成
  },
  onShow:function(){
    // 页面显示
    page = 1;
    selectedNews = false;
    newsList = [];
    this.setData({
      cardlist: []
    })
    this.getCardList();
  },
  onHide:function(){
    // 页面隐藏
  },
  onUnload:function(){
    // 页面关闭
  },
  // onShareAppMessage: function () {
  //   return {
  //     title: '万商',
  //     desc: '你要的名片代',
  //     path: '/pages/home/home'
  //   }
  // },
  /** 
   * 页面上拉触底事件的处理函数 
   */
  onReachBottom: function () {
    if (selectedNews == true){
      var that = this;
      // 显示加载图标  
      wx.showLoading({
        title: '玩命加载中',
      })
      // 页数+1  
      that.getCardList();

    }else{
      wx.hideLoading();
    }
  }
})